/**
 * API文档：http://wechat.hand-china.com/operation-doc/#api-DocumentContentDTO
 */
import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage'

export default {

  //获取目录（树形结构）
  getDocumentMenu() {
    return httpFetch.get(`${config.baseUrl}/api/documentContent/tree`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取文档信息
  getDocumentInfo(id) {
    return httpFetch.get(`${config.baseUrl}/api/documentContent?id=${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //新建／更新文档
  editDocument(params, method) {
    return httpFetch[method](`${config.baseUrl}/api/documentContent`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //上传图片
  handleImageUpload(formData) {
    return httpFetch.post(`${config.baseUrl}/api/upload/static/attachment`, formData)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取历史发布列表
  getHistoryList(params) {
    return httpFetch.get(`${config.baseUrl}/api/documentContent/history`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取历史记录
  getHistoryContent(params) {
    return httpFetch.get(`${config.baseUrl}/api/documentContent/history/id`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //文档发布（所有文档）
  handleRelease() {
    return httpFetch.get(`${config.baseUrl}/api/documentContent/publish`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //目录排序
  handleSort(params) {
    return httpFetch.put(`${config.baseUrl}/api/documentContent/tree/sort`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //导出文档
  exportDocument(params) {
    return  httpFetch.get(`${config.baseUrl}/api/documentContent/export`, params, {}, {responseType: 'arraybuffer'})
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //根据发布类型获取列表
  getMenuListByPublishType(params) {
    return httpFetch.get(`${config.baseUrl}/api/document/publish/type`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  //根据文档id获取最新发布版本
  getPublishTypeById(params) {
    return httpFetch.get(`${config.baseUrl}/api/documentContent/laster/id`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
}
